#ifdef Q_OS_WIN
#include <windows.h>
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")

bool setWindowsTitleBarColor(QWidget *window, QRgb color) {
    if (!window) return false;

    HWND hwnd = reinterpret_cast<HWND>(window->winId());

    // 设置窗口属性以启用DWM渲染
    MARGINS margins = {-1};
    DwmExtendFrameIntoClientArea(hwnd, &margins);

    // 转换颜色
    DWORD dwmColor = RGB(qRed(color), qGreen(color), qBlue(color));

    // 使用DwmSetWindowAttribute替代DwmSetColorizationColor
    BOOL useColor = TRUE;
    DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &useColor, sizeof(BOOL));

    // 设置标题栏颜色（需要Windows 10 1809+）
    BOOL enable = TRUE;
    DwmSetWindowAttribute(hwnd, DWMWA_CAPTION_COLOR, &dwmColor, sizeof(DWORD));

    // 更新窗口
    SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
                 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

    return true;
}

#endif
